package cn.edu.nju.software.cdiy.action.user.login;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import cn.edu.nju.software.cdiy.bean.Scheme;
import cn.edu.nju.software.cdiy.bean.User;
import cn.edu.nju.software.cdiy.service.SchemeService;
import cn.edu.nju.software.cdiy.util.BusinessConstant;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("serial")
public class AddSchemeAction extends ActionSupport implements SessionAware, ServletResponseAware, Preparable {

	private Map<String, Object> session;
	private HttpServletResponse response;
	private SchemeService schemeService;
	private List<String> hardwares;
	private Scheme scheme;

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
		this.response.setCharacterEncoding("UTF-8");
	}
	
	public void setSchemeService(SchemeService schemeService) {
		this.schemeService = schemeService;
	}

	public List<String> getHardwares() {
		return hardwares;
	}
	
	public void setHardwares(List<String> hardwares) {
		this.hardwares = hardwares;
	}
	
	public Scheme getScheme() {
		return scheme;
	}
	
	public void setScheme(Scheme scheme) {
		this.scheme = scheme;
	}

	@Override
	public void prepare() throws Exception {
		if (hardwares != null) {
			hardwares.clear();
		}
		scheme = null;
	}

	public void add() throws Exception {
		if (!hardwares.get(0).equals("0")) {
			String[] splitArray = hardwares.get(0).split("@");
			scheme.setMainBoard(Integer.parseInt(splitArray[0]));
		}
		if (!hardwares.get(1).equals("0")) {
			String[] splitArray = hardwares.get(1).split("@");
			scheme.setCpu(Integer.parseInt(splitArray[0]));
		}
		if (!hardwares.get(2).equals("0")) {
			String[] splitArray = hardwares.get(2).split("@");
			scheme.setMemory(Integer.parseInt(splitArray[0]));
			scheme.setMemoryNum(Integer.parseInt(splitArray[1]));
		}
		if (!hardwares.get(3).equals("0")) {
			String[] splitArray = hardwares.get(3).split("@");
			scheme.setDisk(Integer.parseInt(splitArray[0]));
			scheme.setDiskNum(Integer.parseInt(splitArray[1]));
		}
		if (!hardwares.get(4).equals("0")) {
			String[] splitArray = hardwares.get(4).split("@");
			scheme.setGraphics(Integer.parseInt(splitArray[0]));
		}
		if (!hardwares.get(5).equals("0")) {
			String[] splitArray = hardwares.get(5).split("@");
			scheme.setChassis(Integer.parseInt(splitArray[0]));
		}
		if (!hardwares.get(6).equals("0")) {
			String[] splitArray = hardwares.get(6).split("@");
			scheme.setPower(Integer.parseInt(splitArray[0]));
		}
		if (!hardwares.get(7).equals("0")) {
			String[] splitArray = hardwares.get(7).split("@");
			scheme.setRadiator(Integer.parseInt(splitArray[0]));
		}
		if (!hardwares.get(8).equals("0")) {
			String[] splitArray = hardwares.get(8).split("@");
			scheme.setDriver(Integer.parseInt(splitArray[0]));
		}
		if (!hardwares.get(9).equals("0")) {
			String[] splitArray = hardwares.get(9).split("@");
			scheme.setMonitor(Integer.parseInt(splitArray[0]));
		}
		if (!hardwares.get(10).equals("0")) {
			String[] splitArray = hardwares.get(10).split("@");
			scheme.setSoundBox(Integer.parseInt(splitArray[0]));
		}
		if (!hardwares.get(11).equals("0")) {
			String[] splitArray = hardwares.get(11).split("@");
			scheme.setSoundCard(Integer.parseInt(splitArray[0]));
		}
		if (!hardwares.get(12).equals("0")) {
			String[] splitArray = hardwares.get(12).split("@");
			scheme.setNetworkAdapter(Integer.parseInt(splitArray[0]));
		}
		User user = (User) session.get(BusinessConstant.S_USER);
		scheme.setUserId(user.getId());
		scheme.setUserName(user.getName());
		schemeService.save(scheme);
		response.getWriter().write("提交成功！");
	}
	
}
